package com.wswy.wzcx.ui.other;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.alipay.sdk.app.PayTask;
import com.che.libcommon.utils.RxBus;
import com.che.libcommon.utils.RxHelper;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.model.PaymentOrder;
import com.wswy.wzcx.model.home.RouterUtils;
import com.wswy.wzcx.module.InternalMsgCode;
import com.wswy.wzcx.ui.data.PayResult;
import com.wswy.wzcx.ui.web.WebPageArgument;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import java.util.Map;
import java.util.concurrent.Callable;
import org.json.JSONObject;
import timber.log.Timber;

/**
 * Created by wangduheng26 on 2018/3/5. wangduheng26@gmail.com
 */

public class PayControl {

  /**
   * 拉起微信支付
   */
  public static void requestPayWeChat(final Context context, final String payJson) {
    Single.just(payJson).map(new Function<String, PayReq>() {
      @Override
      public PayReq apply(String s) throws Exception {
        JSONObject json = new JSONObject(s);
        PayReq req = new PayReq();
        req.appId = json.optString("appid");
        req.partnerId = json.optString("partnerid");
        req.prepayId = json.optString("prepayid");
        req.nonceStr = json.optString("noncestr");
        req.timeStamp = json.optString("timestamp");
        req.packageValue = json.optString("package");
        req.sign = json.optString("sign");
        req.extData = "app data"; // optional
        return req;
      }
    }).map(new Function<PayReq, Boolean>() {
      @Override
      public Boolean apply(PayReq payReq) throws Exception {
        return DataCenter.get().getWXApi().sendReq(payReq);
      }
    }).subscribe(new SingleObserver<Boolean>() {
      @Override
      public void onSubscribe(Disposable d) {

      }

      @Override
      public void onSuccess(Boolean aBoolean) {

      }

      @Override
      public void onError(Throwable e) {
        Log.e("wzcx", "wechat", e);
      }
    });


  }

  /**
   * 拉起支付宝
   */
  public static void requestPayAli(final Activity activity, final String payJson) {
    Single.fromCallable(new Callable<PayResult>() {
      @Override
      public PayResult call() throws Exception {

        Map<String, String> result = null;
        try {
          PayTask alipay = new PayTask(activity);
          result = alipay.payV2(payJson, true);
        } catch (Throwable e) {
          e.printStackTrace();
        }

        return PayResult.buildFromAlipay(result);
      }
    }).compose(RxHelper.<PayResult>io2main())
        .subscribe(new SingleObserver<PayResult>() {
          @Override
          public void onSubscribe(Disposable d) {

          }

          @Override
          public void onSuccess(PayResult message) {
            RxBus.getDefault().postWithCode(InternalMsgCode.CODE_PAY_RESULT, message);
          }

          @Override
          public void onError(Throwable e) {
            Timber.e(e);
          }
        });

  }


  public static synchronized void requestPay(final Activity activity,
      final PaymentOrder paymentOrder) {
    WebPageArgument webPageArgument = new WebPageArgument(paymentOrder.getPay());
    webPageArgument.setShowAd(false);
    webPageArgument.setShowBannerAd(false);
    webPageArgument.setShowNestedAd(false);
    webPageArgument.setTitle("订单支付");
    webPageArgument.setShowUrlTitle(false);
    webPageArgument.setReferer(paymentOrder.getReferer());
    webPageArgument.setH5Pay(true);
    RouterUtils.startWeb(activity, webPageArgument);

  }

}
